<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/24
 * Time: 16:10
 */

//异步mysql
class mysql
{


    public $mySqlObj;
    public $config = [];
    public $data;

    public function __construct($str, $data = [])
    {
        $mySqlObj = new swoole_mySQL();
        $this->config = [
            'host' => '120.78.67.1',
            'port' => 3306,
            'user' => 'root',
            'password' => 'liuqiang',
            'database' => 'swoole',
            'charset' => 'utf8', //指定字符集
            'timeout' => 3,  // 可选：连接超时时间（非查询超时时间），默认为SW_MYSQL_CONNECT_TIMEOUT（1.0）
        ];
        $this->data = $data;
        //函数回调
        $mySqlObj->connect($this->config, [$this, $str]);
    }

    public function add(swoole_mysql $db, $result)
    {
        if ($result === false) {
            return '链接失败';
        }
        $key = [];
        $val = [];
        foreach ($this->data as $k => $v) {
            $key[] = $k;
            $val[] = "'{$v}'";
        }
        $key = implode(',', $key);
        $val = implode(',', $val);
        $sql = "INSERT INTO user ($key) VALUES ($val)";
        $db->query($sql, function (swoole_mysql $db, $res) {
            var_dump($res);
            var_dump($db->insert_id);
            $db->close();
        });

    }

    public function del()
    {

    }

    public function select()
    {

    }

    public function update()
    {

    }

}

$con = new mysql('add', ['user' => '刘强', 'password' => 123456]);
//var_dump($con);
//$con->add(['user'=>'liuqiang','password'=>'1111']);